package de.tum.getraenke;

import android.app.ListActivity;
import android.database.Cursor;
import android.os.Bundle;
import android.util.Log;
import android.view.ContextMenu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.view.ContextMenu.ContextMenuInfo;
import android.widget.AdapterView.AdapterContextMenuInfo;

public class SyncActivity extends ListActivity {

	private final static String TAG = SyncActivity.class.getSimpleName();
	
	private DrinkDBHandler dbHandler;
	private Cursor dbCursor;
	private SyncAdapter listAdapter;

	@Override
    public void onCreate(Bundle paramBundle) {
		super.onCreate(paramBundle);
		registerForContextMenu(getListView());
		dbHandler = new DrinkDBHandler(this);
        dbCursor = dbHandler.getOrders();
        startManagingCursor(dbCursor);
        listAdapter = new SyncAdapter(this, dbCursor);
        setListAdapter(listAdapter);	
	}
	
	@Override
	public void onCreateContextMenu(ContextMenu menu, View v,
			ContextMenuInfo menuInfo) {
		super.onCreateContextMenu(menu, v, menuInfo);
		MenuInflater inflater = getMenuInflater();
		inflater.inflate(R.menu.sync_menu, menu);
	}
	
	@Override
	public boolean onContextItemSelected(MenuItem item) {
		AdapterContextMenuInfo info = (AdapterContextMenuInfo) item.getMenuInfo();
		if(item.getItemId() == R.id.delete_order) {
			dbHandler.deleteOrder(info.id);
			updateList();
		}
		return true;
	}
	
	private void updateList() {
		dbCursor.requery();
		listAdapter.notifyDataSetChanged();
	}
}
